﻿using DevExpress.Data.Filtering;
using DevExpress.Xpo;
using Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace BLL.Enums
{
    public class EnumManage
    {
        public static string GetBillStatus(Type enumType, int status)
        {
            FieldInfo fields = enumType.GetField(enumType.GetEnumName(status));
            DevExpress.Xpo.DisplayNameAttribute billStatus =
            (DevExpress.Xpo.DisplayNameAttribute)(fields.GetCustomAttributes(false)[0]);
            return billStatus.DisplayName;
        }

        public static DictionaryEntry[] GetDictionaryEntry(Type enumType)
        {
            Dictionary<int, string> nvc = GetNVCFromEnumValue(enumType);
            DictionaryEntry[] de = new DictionaryEntry[nvc.Count];
            int j = 0;
            foreach (KeyValuePair<int, string> kvp in nvc)
            {
                de[j].Key = kvp.Key;
                //switch (enumType.Name)
                //{
                //    case "ProductType":
                //        de[j].Key = kvp.Key;
                //        break;
                //    case "GrowType":
                //        //de[j].Key = (GrowType)kvp.Key;
                //        break;
                //    case "InventoryStatus":
                //        //de[j].Key = (InventoryStatus)kvp.Key;
                //        break;
                //    case "EnterWarehouseStatus":
                //        //de[j].Key = (EnterWarehouseStatus)kvp.Key;
                //        break;
                //    case "MoveWarehouseStatus":
                //        //de[j].Key = (MoveWarehouseStatus)kvp.Key;
                //        break;
                //    case "AllocationStatus":
                //        //de[j].Key = (AllocationStatus)kvp.Key;
                //        break;
                //    case "AllocationMode":
                //        //de[j].Key = (AllocationMode)kvp.Key;
                //        break;
                //    case "AdjustStatus":
                //        //de[j].Key = (AdjustStatus)kvp.Key;
                //        break;
                //    case "LossStatus":
                //        //de[j].Key = (LossStatus)kvp.Key;
                //        break;
                //    case "ItemType":
                //        //de[j].Key = (ItemType)kvp.Key;
                //        break;
                //    case "OrderType":
                //        de[j].Key = (OrderType)kvp.Key;
                //        break;
                //    case "PaymentStatus":
                //        //de[j].Key = (PaymentStatus)kvp.Key;
                //        break;
                //    case "OrderStatus":
                //        //de[j].Key = (OrderStatus)kvp.Key;
                //        break;
                //    case "PayType":
                //        //de[j].Key = (PayType)kvp.Key;
                //        break;
                //    case "StockChangBillType":
                //        //de[j].Key = (StockChangBillType)kvp.Key;
                //        break;
                //}
                de[j].Value = kvp.Value;
                j++;
            }
            return de;
        }
        public static DictionaryEntry[] GetDictionaryEntry(BaseEnum enumType)
        {
            XPCollection<S_Parameter> par = new XPCollection<S_Parameter>(DatabaseSchema.Session, CriteriaOperator.Parse("TypeID=?", (int)enumType));
            DictionaryEntry[] de = new DictionaryEntry[par.Count];
            int j = 0;
            foreach (S_Parameter p in par) 
            {
                de[j].Key = p.ParameterID;
                de[j].Value = p.ParameterValue;
                j++;
            }
            return de;
        }
        static Dictionary<int, string> GetNVCFromEnumValue(Type enumType)
        {
            Dictionary<int, string> kvp = new Dictionary<int, string>();
            Type typeDescription = typeof(DevExpress.Xpo.DisplayNameAttribute);
            System.Reflection.FieldInfo[] fields = enumType.GetFields();
            string strText = string.Empty;
            int strValue;
            foreach (FieldInfo field in fields)
            {
                if (field.FieldType.IsEnum)
                {
                    strValue = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null));
                    object[] arr = field.GetCustomAttributes(typeDescription, true);
                    if (arr.Length > 0)
                    {
                        DevExpress.Xpo.DisplayNameAttribute dna = (DevExpress.Xpo.DisplayNameAttribute)arr[0];
                        strText = dna.DisplayName;
                    }
                    else
                        strText = field.Name;
                    kvp.Add(strValue, strText);
                }
            }
            return kvp;
        }
    }
}
